// 获取 DOM 元素
const editInfoBtn = document.getElementById('edit-info-btn');
const saveInfoBtn = document.getElementById('save-info-btn');
const usernameInput = document.getElementById('username');
const phoneInput = document.getElementById('phone');
const addressInput = document.getElementById('address');
const memberLevelSpan = document.getElementById('member-level');
const pointsSpan = document.getElementById('points');
const shoppingCartList = document.getElementById('shopping-cart-list');
const orderList = document.getElementById('order-list');

// 模拟购物车数据
const shoppingCartData = [
    { name: '小米11 Ultra', price: 6499 },
];

// 模拟订单数据
const orderData = [
    {
        orderId: '123456',
        products: [
            { name: '小米11 Ultra', price: 6499 },
      
        ],
        totalPrice: 6499,
        status: '已完成'
    },
    {
        orderId: '654321',
        products: [
            { name: '键盘', price: 299 },
            { name: '鼠标', price: 99 }
        ],
        totalPrice: 398,
        status: '待付款'
    }
];

// 显示购物车内容
function displayShoppingCart() {
    shoppingCartList.innerHTML = '';
    shoppingCartData.forEach(item => {
        const li = document.createElement('li');
        li.textContent = `${item.name} - ￥${item.price}`;
        shoppingCartList.appendChild(li);
    });
}

// 显示订单详情
function displayOrderDetails() {
    orderList.innerHTML = '';
    orderData.forEach(order => {
        const li = document.createElement('li');
        li.innerHTML = `
            <p>订单编号：${order.orderId}</p>
            <p>商品列表：</p>
            <ul>
                ${order.products.map(product => `<li>${product.name} - ￥${product.price}</li>`).join('')}
            </ul>
            <p>总价：￥${order.totalPrice}</p>
            <p>订单状态：${order.status}</p>
        `;
        orderList.appendChild(li);
    });
}

// 编辑信息按钮点击事件
editInfoBtn.addEventListener('click', () => {
    usernameInput.readOnly = false;
    phoneInput.readOnly = false;
    addressInput.readOnly = false;
    editInfoBtn.style.display = 'none';
    saveInfoBtn.style.display = 'inline-block';
});

// 保存信息按钮点击事件
saveInfoBtn.addEventListener('click', () => {
    usernameInput.readOnly = true;
    phoneInput.readOnly = true;
    addressInput.readOnly = true;
    editInfoBtn.style.display = 'inline-block';
    saveInfoBtn.style.display = 'none';
    // 这里可以添加保存信息到后端的逻辑
    console.log('保存的用户名：', usernameInput.value);
    console.log('保存的手机号：', phoneInput.value);
    console.log('保存的收货地址：', addressInput.value);
});

// 页面加载完成后显示购物车内容和订单详情
window.addEventListener('load', () => {
    displayShoppingCart();
    displayOrderDetails();
});

// 模拟会员等级和积分更新
function updateMemberLevelAndPoints() {
    // 这里可以根据实际业务逻辑更新会员等级和积分
    // 例如根据订单数量或消费金额来更新
    const totalOrders = orderData.length;
    if (totalOrders >= 5) {
        memberLevelSpan.textContent = '高级会员';
    } else if (totalOrders >= 2) {
        memberLevelSpan.textContent = '中级会员';
    } else {
        memberLevelSpan.textContent = '普通会员';
    }

    // 假设每消费 1 元获得 1 积分
    let totalPoints = 0;
    orderData.forEach(order => {
        totalPoints += order.totalPrice;
    });
    pointsSpan.textContent = totalPoints;
}

// 页面加载完成后更新会员等级和积分
window.addEventListener('load', updateMemberLevelAndPoints);